package com.nbjtjc.safe.service.safe;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.bean.RiskItemRowspan;
import com.nbjtjc.safe.mapper.safe.RiskMarkItemMapper;
import com.nbjtjc.safe.mapper.safe.RiskPointItemMapper;
import com.nbjtjc.safe.mapper.safe.RiskPointMapper;
import com.nbjtjc.safe.model.safe.RiskMark;
import com.nbjtjc.safe.model.safe.RiskMarkItem;
import com.nbjtjc.safe.model.safe.RiskPoint;
import com.nbjtjc.safe.model.safe.RiskPointItem;
import com.nbjtjc.safe.util.MyShiroUtils;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.*;
import java.util.HashMap;
import java.util.stream.Collectors;

@Service
public class RiskPointItemServiceImpl {

    @Autowired
    private RiskPointItemMapper riskPointItemMapper;

    @Autowired
    private RiskMarkItemMapper riskMarkItemMapper;

    @Autowired
    private RiskPointMapper riskPointMapper;

    public Result find(RiskPointItem riskPointItem) {
        Result result = new Result();
        PageHelper.startPage(riskPointItem.getPageCurrent(), riskPointItem.getPageSize());
        List<RiskPointItem> riskPointItems = riskPointItemMapper.search(riskPointItem);
//        compareRowspan(riskPointItems);
        compareRowspanNew(riskPointItems);
        compareIconUrl(riskPointItems);
        compareResultBase(riskPointItems);
        result.setData(new PageInfo<>(riskPointItems));
        return result;
    }

    public void compareResultBase(List<RiskPointItem> riskPointItems) {
        riskPointItems.forEach(item -> {
            if (StringUtils.isNotEmpty(item.getEvaluationResultBase())
                    && JSONUtil.isJson(item.getEvaluationResultBase())
            ) {
                try {
                    JSONObject resultObj=null;
                    if (JSONUtil.isJsonArray(item.getEvaluationResultBase())) {
                        resultObj = JSON.parseArray(item.getEvaluationResultBase()).getJSONObject(0);
                    } else {
                        resultObj = JSON.parseObject(item.getEvaluationResultBase());
                    }
                    String resultStr="D("+resultObj.getString("D")+")=L("+resultObj.getString("L")
                            +")*E("+resultObj.getString("E")+")*C("+resultObj.getString("C")+")";
                    item.setResultBaseStr(resultStr);
                    item.setL(resultObj.getString("L"));
                    item.setE(resultObj.getString("E"));
                    item.setC(resultObj.getString("C"));
                    if (NumberUtil.isNumber(resultObj.getString("D"))) {
                        int result = NumberUtil.parseInt(resultObj.getString("D"));
                        if (result < 70) {
                            item.setResultBackColor("#34B6F5");
                        }
                        if (result >= 70 & result < 160) {
                            item.setResultBackColor("#FBFF00");
                        }
                        if (result >= 160 & result < 320) {
                            item.setResultBackColor("#FBCA00");
                        }
                        if (result > 320) {
                            item.setResultBackColor("#FA0000");
                        }
                    }
                } catch (Exception e) {
                    System.out.println(e.getStackTrace());
                }
            }
        });
    }

    /**
     * 查询item的图片标识
     *
     * @param riskPointItems
     */
    public void compareIconUrl(List<RiskPointItem> riskPointItems) {
        List<Integer> riskPointItemIds = riskPointItems.stream().map(RiskPointItem::getId).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(riskPointItemIds)) {
            return;
        }
        List<RiskMarkItem> riskMarkItems = riskMarkItemMapper.findByRiskPointIds(riskPointItemIds);
        if (CollectionUtil.isEmpty(riskMarkItems)) {
            return;
        }
        Map<Integer, List<RiskMarkItem>> markMap = new HashMap<>();
        riskMarkItems.forEach(mark -> {
            if (markMap.containsKey(mark.getItemId())) {
                markMap.get(mark.getItemId()).add(mark);
            } else {
                List<RiskMarkItem> riskMarkItemList = new ArrayList<>();
                riskMarkItemList.add(mark);
                markMap.put(mark.getItemId(), riskMarkItemList);
            }
        });
        riskPointItems.forEach(pointItem -> {
            if (markMap.containsKey(pointItem.getId())) {
                pointItem.setRiskMarkItemList(markMap.get(pointItem.getId()));
            }
        });

    }

    class Rowspan{
        String key;
        String value;
        int start;
        int end;

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public int getStart() {
            return start;
        }

        public void setStart(int start) {
            this.start = start;
        }

        public int getEnd() {
            return end;
        }

        public void setEnd(int end) {
            this.end = end;
        }
    }
    public void compareRowspanNew(List<RiskPointItem> riskPointItems){
        HashMap<String, List<Rowspan>> stepOrUnitMap = new HashMap<>();
        HashMap<String, List<Rowspan>> dangerousSourceMap = new HashMap<>();
        HashMap<String, List<Rowspan>> latentConsequencesNameMap = new HashMap<>();
        HashMap<String, List<Rowspan>> accidentNameMap = new HashMap<>();
        HashMap<String, List<Rowspan>> evaluationDirectNameMap = new HashMap<>();
        HashMap<String, List<Rowspan>> controlLevelMap = new HashMap<>();
        HashMap<String, List<Rowspan>> inducedCauseMap = new HashMap<>();
        HashMap<String, List<Rowspan>> controlMeasuresMap = new HashMap<>();
        HashMap<String, List<Rowspan>> lawReferenceMap = new HashMap<>();
        HashMap<String, List<Rowspan>> manageMeasuresMap = new HashMap<>();
        HashMap<String, List<Rowspan>> trainMeasuresMap = new HashMap<>();
        HashMap<String, List<Rowspan>> emergencyMeasuresMap = new HashMap<>();
        HashMap<String, List<Rowspan>> evaluationResultValueMap = new HashMap<>();
        for(int i=0;i<riskPointItems.size();i++){
            RiskPointItem pointItem=riskPointItems.get(i);
            if (stepOrUnitMap.containsKey(pointItem.getStepOrUnit())) {
                List<Rowspan> indexList=stepOrUnitMap.get(pointItem.getStepOrUnit());
                Rowspan rowspan=indexList.get(indexList.size()-1);
                if(rowspan.end+1==i){
                    rowspan.setEnd(i);
                }else{
                    Rowspan rowspanNew=new Rowspan();
                    rowspanNew.setStart(i);
                    rowspanNew.setEnd(i);
                    rowspanNew.setKey(pointItem.getStepOrUnit());
                    indexList.add(rowspanNew);
                }
            } else {
                List<Rowspan> rowspans=new ArrayList<>();
                Rowspan rowspan=new Rowspan();
                rowspan.setKey(pointItem.getStepOrUnit());
                rowspan.setStart(i);
                rowspan.setEnd(i);
                rowspans.add(rowspan);
                stepOrUnitMap.put(pointItem.getStepOrUnit(),rowspans);
            }
            String dangerousSourceKey=pointItem.getStepOrUnit()+"-"+pointItem.getDangerousSource();
            if(dangerousSourceMap.containsKey(dangerousSourceKey)){
                List<Rowspan> rowspanList=dangerousSourceMap.get(dangerousSourceKey);
                Rowspan rowspan=rowspanList.get(rowspanList.size()-1);
                if(rowspan.end+1==i){
                    rowspan.setEnd(i);
                }else{
                    Rowspan rowspanNew=new Rowspan();
                    rowspanNew.setStart(i);
                    rowspanNew.setEnd(i);
                    rowspanNew.setKey(dangerousSourceKey);
                    rowspanList.add(rowspanNew);
                }
            }else{
                List<Rowspan> rowspans=new ArrayList<>();
                Rowspan rowspan=new Rowspan();
                rowspan.setKey(dangerousSourceKey);
                rowspan.setStart(i);
                rowspan.setEnd(i);
                rowspans.add(rowspan);
                dangerousSourceMap.put(dangerousSourceKey,rowspans);
            }
            String inducedCauseKey=pointItem.getStepOrUnit()+"-"+pointItem.getDangerousSource()+"-"+pointItem.getInducedCause();
            if(inducedCauseMap.containsKey(inducedCauseKey)){
                List<Rowspan> rowspanList=inducedCauseMap.get(inducedCauseKey);
                Rowspan rowspan=rowspanList.get(rowspanList.size()-1);
                if(rowspan.end+1==i){
                    rowspan.setEnd(i);
                }else{
                    Rowspan rowspanNew=new Rowspan();
                    rowspanNew.setStart(i);
                    rowspanNew.setEnd(i);
                    rowspanNew.setKey(inducedCauseKey);
                    rowspanList.add(rowspanNew);
                }
            }else{
                List<Rowspan> rowspans=new ArrayList<>();
                Rowspan rowspan=new Rowspan();
                rowspan.setKey(inducedCauseKey);
                rowspan.setStart(i);
                rowspan.setEnd(i);
                rowspans.add(rowspan);
                inducedCauseMap.put(inducedCauseKey,rowspans);
            }
            String controlMeasuresKey=pointItem.getStepOrUnit()+"-"+pointItem.getDangerousSource()
                    +"-"+pointItem.getInducedCause()+"-"+pointItem.getControlMeasures();
            if(controlMeasuresMap.containsKey(controlMeasuresKey)){
                List<Rowspan> rowspanList=controlMeasuresMap.get(controlMeasuresKey);
                Rowspan rowspan=rowspanList.get(rowspanList.size()-1);
                if(rowspan.end+1==i){
                    rowspan.setEnd(i);
                }else{
                    Rowspan rowspanNew=new Rowspan();
                    rowspanNew.setStart(i);
                    rowspanNew.setEnd(i);
                    rowspanNew.setKey(controlMeasuresKey);
                    rowspanList.add(rowspanNew);
                }
            }else{
                List<Rowspan> rowspans=new ArrayList<>();
                Rowspan rowspan=new Rowspan();
                rowspan.setKey(controlMeasuresKey);
                rowspan.setStart(i);
                rowspan.setEnd(i);
                rowspans.add(rowspan);
                controlMeasuresMap.put(controlMeasuresKey,rowspans);
            }
        }
        List<Rowspan> stepOrUnitList=new ArrayList<>();
        for(Map.Entry<String,List<Rowspan>> entry :stepOrUnitMap.entrySet()){
            stepOrUnitList.addAll(entry.getValue());
        }

        List<Rowspan> dangerousSourceList=new ArrayList<>();
        for(Map.Entry<String,List<Rowspan>> entry :dangerousSourceMap.entrySet()){
            dangerousSourceList.addAll(entry.getValue());
        }
        List<Rowspan> inducedCauseList=new ArrayList<>();
        for(Map.Entry<String,List<Rowspan>> entry :inducedCauseMap.entrySet()){
            inducedCauseList.addAll(entry.getValue());
        }
        List<Rowspan> controlMeasuresList=new ArrayList<>();
        for(Map.Entry<String,List<Rowspan>> entry :controlMeasuresMap.entrySet()){
            controlMeasuresList.addAll(entry.getValue());
        }
        List<Integer> stepOrUnitRowspanList=indexRange2List(stepOrUnitList);
        List<Integer> dangerousSourceRowspanList=indexRange2List(dangerousSourceList);
        List<Integer> inducedCauseRowspanList=indexRange2List(inducedCauseList);
        List<Integer> controlMeasuresRowspanList=indexRange2List(controlMeasuresList);

        for(int i=0;i<riskPointItems.size();i++){
            RiskPointItem riskPointItem=riskPointItems.get(i);
            riskPointItem.getRowspanList().add(stepOrUnitRowspanList.get(i));
            riskPointItem.getRowspanList().add(dangerousSourceRowspanList.get(i));
            riskPointItem.getRowspanList().add(dangerousSourceRowspanList.get(i));
            riskPointItem.getRowspanList().add(dangerousSourceRowspanList.get(i));
            riskPointItem.getRowspanList().add(dangerousSourceRowspanList.get(i));
            riskPointItem.getRowspanList().add(dangerousSourceRowspanList.get(i));
            riskPointItem.getRowspanList().add(inducedCauseRowspanList.get(i));
            riskPointItem.getRowspanList().add(controlMeasuresRowspanList.get(i));
            riskPointItem.getRowspanList().add(controlMeasuresRowspanList.get(i));
            riskPointItem.getRowspanList().add(dangerousSourceRowspanList.get(i));
            riskPointItem.getRowspanList().add(dangerousSourceRowspanList.get(i));
            riskPointItem.getRowspanList().add(dangerousSourceRowspanList.get(i));
            riskPointItem.getRowspanList().add(dangerousSourceRowspanList.get(i));
            riskPointItem.getRowspanList().add(dangerousSourceRowspanList.get(i));
            riskPointItem.getRowspanList().add(dangerousSourceRowspanList.get(i));
            riskPointItem.getRowspanList().add(1);
//            riskPointItem.getRowspanList().add(1);
        }
    }

    public List<Integer> indexRange2List(List<Rowspan> rowspanList){
        List<Integer> indexList=new ArrayList<>();
        rowspanList.sort(Comparator.comparing(Rowspan::getStart));
        rowspanList.forEach(rowspan -> {
            int range=rowspan.getEnd()-rowspan.getStart()+1;
            indexList.add(range);
            for(int i=0;i<range-1;i++) {
                indexList.add(0);
            }
        });
        return indexList;
    }

    public void compareRowspan(List<RiskPointItem> riskPointItems) {
        Map<String, RiskItemRowspan> rowspanMap = new HashMap<>();
        HashMap<String, Integer> stepOrUnitMap = new HashMap<>();
        HashMap<String, Integer> dangerousSourceMap = new HashMap<>();
        HashMap<String, Integer> latentConsequencesNameMap = new HashMap<>();
        HashMap<String, Integer> accidentNameMap = new HashMap<>();
        HashMap<String, Integer> evaluationDirectNameMap = new HashMap<>();
        HashMap<String, Integer> controlLevelMap = new HashMap<>();
        HashMap<String, Integer> inducedCauseMap = new HashMap<>();
        HashMap<String, Integer> controlMeasuresMap = new HashMap<>();
        HashMap<String, Integer> lawReferenceMap = new HashMap<>();
        HashMap<String, Integer> manageMeasuresMap = new HashMap<>();
        HashMap<String, Integer> trainMeasuresMap = new HashMap<>();
        HashMap<String, Integer> emergencyMeasuresMap = new HashMap<>();
        HashMap<String, Integer> evaluationResultValueMap = new HashMap<>();
        riskPointItems.forEach(pointItem -> {
            if (stepOrUnitMap.containsKey(pointItem.getStepOrUnit())) {
                stepOrUnitMap.put(pointItem.getStepOrUnit()
                        , 1 + stepOrUnitMap.get(pointItem.getStepOrUnit()));
            } else {
                stepOrUnitMap.put(pointItem.getStepOrUnit(), 1);
            }
            String dangerousSource = pointItem.getDangerousSource();
            if (dangerousSourceMap.containsKey(dangerousSource)) {
                dangerousSourceMap.put(dangerousSource, 1 + dangerousSourceMap.get(dangerousSource));
            } else {
                dangerousSourceMap.put(dangerousSource, 1);
            }
            String latentConsequencesName = pointItem.getLatentConsequencesName();
            if (latentConsequencesNameMap.containsKey(latentConsequencesName)) {
                latentConsequencesNameMap.put(latentConsequencesName,
                        latentConsequencesNameMap.get(latentConsequencesName) + 1);
            } else {
                latentConsequencesNameMap.put(latentConsequencesName, 1);
            }
            String accidentName = pointItem.getAccidentName();
            if (accidentNameMap.containsKey(accidentName)) {
                accidentNameMap.put(accidentName, accidentNameMap.get(accidentName) + 1);
            } else {
                accidentNameMap.put(accidentName, 1);
            }
            String evaluationDirectName = pointItem.getEvaluationDirectName();
            if (evaluationDirectNameMap.containsKey(evaluationDirectName)) {
                evaluationDirectNameMap.put(evaluationDirectName
                        , evaluationDirectNameMap.get(evaluationDirectName) + 1);
            } else {
                evaluationDirectNameMap.put(evaluationDirectName, 1);
            }
            String controlLevel = pointItem.getControlLevel();
            if (controlLevelMap.containsKey(controlLevel)) {
                controlLevelMap.put(controlLevel
                        , controlLevelMap.get(controlLevel) + 1);
            } else {
                controlLevelMap.put(controlLevel, 1);
            }
            String inducedCause = pointItem.getInducedCause();
            if (inducedCauseMap.containsKey(inducedCause)) {
                inducedCauseMap.put(inducedCause
                        , inducedCauseMap.get(inducedCause) + 1);
            } else {
                inducedCauseMap.put(inducedCause, 1);
            }
            String controlMeasuresKey = pointItem.getInducedCause()+"-"+ pointItem.getControlMeasures();
            if (controlMeasuresMap.containsKey(controlMeasuresKey)) {
                controlMeasuresMap.put(controlMeasuresKey
                        , controlMeasuresMap.get(controlMeasuresKey) + 1);
            } else {
                controlMeasuresMap.put(controlMeasuresKey, 1);
            }
            String lawReference = pointItem.getLawReference();
            if (lawReferenceMap.containsKey(lawReference)) {
                lawReferenceMap.put(lawReference
                        , lawReferenceMap.get(lawReference) + 1);
            } else {
                lawReferenceMap.put(lawReference, 1);
            }
            String manageMeasures = pointItem.getManageMeasures();
            if (manageMeasuresMap.containsKey(manageMeasures)) {
                manageMeasuresMap.put(manageMeasures
                        , manageMeasuresMap.get(manageMeasures) + 1);
            } else {
                manageMeasuresMap.put(manageMeasures, 1);
            }
            String trainMeasures = pointItem.getTrainMeasures();
            if (trainMeasuresMap.containsKey(trainMeasures)) {
                trainMeasuresMap.put(trainMeasures
                        , trainMeasuresMap.get(trainMeasures) + 1);
            } else {
                trainMeasuresMap.put(trainMeasures, 1);
            }
            String emergencyMeasures = pointItem.getEmergencyMeasures();
            if (emergencyMeasuresMap.containsKey(emergencyMeasures)) {
                emergencyMeasuresMap.put(emergencyMeasures
                        , emergencyMeasuresMap.get(emergencyMeasures) + 1);
            } else {
                emergencyMeasuresMap.put(emergencyMeasures, 1);
            }
            String evaluationResultValue = pointItem.getEvaluationResultValue();
            if (evaluationResultValueMap.containsKey(evaluationResultValue)) {
                evaluationResultValueMap.put(evaluationResultValue
                        , evaluationResultValueMap.get(evaluationResultValue) + 1);
            } else {
                evaluationResultValueMap.put(evaluationResultValue, 1);
            }
        });
        HashSet<String> stepOrUnitSet = new HashSet<>();
        HashSet<String> dangerousSourceSet = new HashSet<>();
        HashSet<String> latentConsequencesNameSet = new HashSet<>();
        HashSet<String> accidentNameSet = new HashSet<>();
        HashSet<String> evaluationDirectNameSet = new HashSet<>();
        HashSet<String> controlLevelSet = new HashSet<>();
        HashSet<String> inducedCauseSet = new HashSet<>();
        HashSet<String> controlMeasuresSet = new HashSet<>();
        HashSet<String> lawReferenceSet = new HashSet<>();
        HashSet<String> manageMeasuresSet = new HashSet<>();
        HashSet<String> trainMeasuresSet = new HashSet<>();
        HashSet<String> emergencyMeasuresSet = new HashSet<>();
        HashSet<String> evaluationResultValueSet = new HashSet<>();
        riskPointItems.forEach(pointItem -> {
            if (!stepOrUnitSet.contains(pointItem.getStepOrUnit())) {
                pointItem.getRowspanList().add(stepOrUnitMap.get(pointItem.getStepOrUnit()));
                stepOrUnitSet.add(pointItem.getStepOrUnit());
            } else {
                pointItem.getRowspanList().add(0);
            }
            if (!dangerousSourceSet.contains(pointItem.getDangerousSource())) {
                pointItem.getRowspanList().add(dangerousSourceMap.get(pointItem.getDangerousSource()));
                pointItem.getRowspanList().add(dangerousSourceMap.get(pointItem.getDangerousSource()));
                pointItem.getRowspanList().add(dangerousSourceMap.get(pointItem.getDangerousSource()));
                pointItem.getRowspanList().add(dangerousSourceMap.get(pointItem.getDangerousSource()));
                pointItem.getRowspanList().add(dangerousSourceMap.get(pointItem.getDangerousSource()));
                dangerousSourceSet.add(pointItem.getDangerousSource());
            } else {
                pointItem.getRowspanList().add(0);
                pointItem.getRowspanList().add(0);
                pointItem.getRowspanList().add(0);
                pointItem.getRowspanList().add(0);
                pointItem.getRowspanList().add(0);
            }
//            if(!latentConsequencesNameSet.contains(pointItem.getLatentConsequencesName())){
//                pointItem.getRowspanList().add(
//                        latentConsequencesNameMap.get(pointItem.getLatentConsequencesName()));
//                latentConsequencesNameSet.add(pointItem.getLatentConsequencesName());
//            }else{
//                pointItem.getRowspanList().add(0);
//            }
//            if(!accidentNameSet.contains(pointItem.getAccidentName())){
//                pointItem.getRowspanList().add(accidentNameMap.get(pointItem.getAccidentName()));
//                accidentNameSet.add(pointItem.getAccidentName());
//            }else{
//                pointItem.getRowspanList().add(0);
//            }
//            if(!evaluationDirectNameSet.contains(pointItem.getEvaluationDirectName())){
//                pointItem.getRowspanList().add(
//                        evaluationDirectNameMap.get(pointItem.getEvaluationDirectName()));
//                pointItem.getRowspanList().add(
//                        evaluationDirectNameMap.get(pointItem.getEvaluationDirectName()));
//                evaluationDirectNameSet.add(pointItem.getEvaluationDirectName());
//            }else{
//                pointItem.getRowspanList().add(0);
//            }
//            if(!controlLevelSet.contains(pointItem.getControlLevel())){
//                pointItem.getRowspanList().add(
//                        controlLevelMap.get(pointItem.getControlLevel()));
//                controlLevelSet.add(pointItem.getControlLevel());
//            }else{
//                pointItem.getRowspanList().add(0);
//            }
            if (!inducedCauseSet.contains(pointItem.getInducedCause())) {
                pointItem.getRowspanList().add(
                        inducedCauseMap.get(pointItem.getInducedCause()));
                inducedCauseSet.add(pointItem.getInducedCause());
            } else {
                pointItem.getRowspanList().add(0);
            }
            String controlMeasuresKey=pointItem.getInducedCause()+"-"+pointItem.getControlMeasures();
            if (!controlMeasuresSet.contains(controlMeasuresKey)) {
                pointItem.getRowspanList().add(controlMeasuresMap.get(controlMeasuresKey));
                pointItem.getRowspanList().add(controlMeasuresMap.get(controlMeasuresKey));
                controlMeasuresSet.add(controlMeasuresKey);
            } else {
                pointItem.getRowspanList().add(0);
                pointItem.getRowspanList().add(0);
            }
//            if(!lawReferenceSet.contains(pointItem.getLawReference())){
//                pointItem.getRowspanList().add(
//                        lawReferenceMap.get(pointItem.getLawReference()));
//                lawReferenceSet.add(pointItem.getLawReference());
//            }else{
//                pointItem.getRowspanList().add(0);
//            }
            pointItem.getRowspanList().add(pointItem.getRowspanList().get(1));
            pointItem.getRowspanList().add(pointItem.getRowspanList().get(1));
            pointItem.getRowspanList().add(pointItem.getRowspanList().get(1));
            pointItem.getRowspanList().add(pointItem.getRowspanList().get(1));
            pointItem.getRowspanList().add(pointItem.getRowspanList().get(1));
            pointItem.getRowspanList().add(pointItem.getRowspanList().get(1));
            pointItem.getRowspanList().add(pointItem.getRowspanList().get(1));
//            if(!manageMeasuresSet.contains(pointItem.getManageMeasures())){
//                pointItem.getRowspanList().add(
//                        manageMeasuresMap.get(pointItem.getManageMeasures()));
//                manageMeasuresSet.add(pointItem.getManageMeasures());
//            }else{
//                pointItem.getRowspanList().add(0);
//            }
//            if(!trainMeasuresSet.contains(pointItem.getTrainMeasures())){
//                pointItem.getRowspanList().add(
//                        trainMeasuresMap.get(pointItem.getTrainMeasures()));
//                trainMeasuresSet.add(pointItem.getTrainMeasures());
//            }else{
//                pointItem.getRowspanList().add(0);
//            }
//            if(!emergencyMeasuresSet.contains(pointItem.getEmergencyMeasures())){
//                pointItem.getRowspanList().add(
//                        emergencyMeasuresMap.get(pointItem.getEmergencyMeasures()));
//                emergencyMeasuresSet.add(pointItem.getEmergencyMeasures());
//            }else{
//                pointItem.getRowspanList().add(0);
//            }
//            if(!evaluationResultValueSet.contains(pointItem.getEvaluationResultValue())){
//                pointItem.getRowspanList().add(
//                        evaluationResultValueMap.get(pointItem.getEvaluationResultValue()));
//                emergencyMeasuresSet.add(pointItem.getEvaluationResultValue());
//            }else{
//                pointItem.getRowspanList().add(0);
//            }
            System.out.println(pointItem.getRowspanList().toArray().toString());
        });

    }

    public Result add(RiskPointItem riskPointItem) {
        Result result = new Result();
        riskPointItem.setCreateUserId(MyShiroUtils.getSessionUserId());
        riskPointItemMapper.insertSelective(riskPointItem);
        result.setData(riskPointItem);
        return result;
    }

    @Transactional
    public Result addBatch(List<RiskPointItem> riskPointItems) {
        Result result = new Result();
        if (CollectionUtil.isNotEmpty(riskPointItems)) {
            riskPointItems.forEach(item -> {
                item.setId(null);
                item.setCreateTime(new Date());
                item.setCreateUserId(MyShiroUtils.getSessionUserId());
            });
            riskPointItemMapper.insertList(riskPointItems);
        }
        return result;
    }

    @Transactional
    public Result saveBatch(List<RiskPointItem> riskPointItems) {
        Result result = new Result();
        if (CollectionUtil.isNotEmpty(riskPointItems)) {
            riskPointItemMapper.deleteByRiskPointId(riskPointItems.get(0).getRiskPointId());
            riskPointItems.forEach(item -> {
                item.setId(null);
                item.setCreateTime(new Date());
                item.setCreateUserId(MyShiroUtils.getSessionUserId());
            });
            riskPointItemMapper.insertList(riskPointItems);
        }
        return result;
    }

    @Transactional
    public Result saveBatchNeedUpdate(List<RiskPointItem> riskPointItems) {
        Result result = new Result();
        if (CollectionUtil.isNotEmpty(riskPointItems)) {
            List<RiskPointItem> addList=new ArrayList<>();
            List<RiskPointItem> deleteList=new ArrayList<>();
            int count=riskPointItems.size();
            int pointId=0;
            for(RiskPointItem item : riskPointItems){
                item.setEvaluationResultType("LEC");
                if (ObjectUtil.isNotNull(item.getId())) {
                    if(item.getDelete()){
                        deleteList.add(item);
                    }else{
                        pointId=item.getRiskPointId();
                        item.setEditTime(new Date());
                        item.setEditUserId(MyShiroUtils.getSessionUserId());
                        compareResultBase(item);
                        riskPointItemMapper.updateByPrimaryKeySelective(item);
                    }
                }
                if(ObjectUtil.isNull(item.getId())){
                    item.setCreateTime(new Date());
                    compareResultBase(item);
                    item.setCreateUserId(MyShiroUtils.getSessionUserId());
                    addList.add(item);
                }
            }
            if(CollectionUtil.isNotEmpty(addList)){
                riskPointItemMapper.insertList(addList);
            }
            if(CollectionUtil.isNotEmpty(deleteList)){
                deleteList.forEach(pointItem -> {
                    riskPointItemMapper.deleteByPrimaryKey(pointItem.getId());
                });
            }
            count=count-deleteList.size();
            riskPointMapper.updateCountByPrimaryKey(count,pointId);
        }
        result.setData(riskPointItems);
        return result;
    }


    /**
     * 设置
     * @param item
     */
    public  void compareResultBase(RiskPointItem item){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("L",item.getL());
        jsonObject.put("E",item.getE());
        jsonObject.put("C",item.getC());
        DecimalFormat df = new DecimalFormat("#.0");
        String d="";
        if(NumberUtil.isNumber(item.getL())&&NumberUtil.isNumber(item.getE())&&NumberUtil.isNumber(item.getC())){
            d= df.format(Double.parseDouble(item.getL())*Double.parseDouble(item.getE())*Double.parseDouble(item.getC()));
            jsonObject.put("D",d);
            item.setEvaluationResultBase(jsonObject.toJSONString());
            int result=NumberUtil.parseInt(d);
            if (result < 20&&result>=0) {
                item.setEvaluationResultName("稍有风险");
                item.setControlLevel("无需特殊管控");
            }
            if (result >=20&&result<700) {
                item.setEvaluationResultName("低风险");
                item.setControlLevel("班组级或者岗位级");
            }

            if (result >= 70 & result < 160) {
                item.setEvaluationResultName("一般风险");
                item.setControlLevel("部门级");
            }
            if (result >= 160 & result < 320) {
                item.setEvaluationResultName("较大风险");
                item.setControlLevel("公司级");
            }
            if (result > 320) {
                item.setEvaluationResultName("重大风险");
                item.setControlLevel("公司级");
            }
        }

    }

    public void setEvaluationName(RiskPointItem item){

    }

    @Transactional
    public Result delete(RiskPointItem riskPointItem) {
        Result result = new Result();
        if (ObjectUtil.isNull(riskPointItem.getId())) {
            return result;
        }
        RiskPointItem item = riskPointItemMapper.selectByPrimaryKey(riskPointItem.getId());
        riskPointItemMapper.deleteByPrimaryKey(riskPointItem.getId());
        RiskPoint riskPoint = riskPointMapper.selectByPrimaryKey(item.getId());
        int count = riskPointItemMapper.countItemByPointId(riskPoint.getId());
        riskPoint.setItemCount(count);
        riskPointMapper.updateByPrimaryKeySelective(riskPoint);
        return result;
    }

    @Transactional
    public Result update(RiskPointItem riskPointItem) {
        Result result = new Result();
        riskPointItem.setEditUserId(MyShiroUtils.getSessionUserId());
        riskPointItemMapper.updateByPrimaryKeySelective(riskPointItem);
        result.setData(riskPointItem);
        return result;
    }
}
